/*
 * @title: vuex定义module：user信息全局变量
 * @author: andy.ten@tom.com & xukaixing@hotmail.com
 * @Date: 2022-03-23 17:11:48
 * @LastEditors: andy.ten@tom.com
 * @LastEditTime: 2022-06-06 11:50:13
 * @version: 1.0.8
 * @copyright: copyright (c) 2022 andyten
 *  node: you can not copy and/or use and/or modify this program free,
 *  please reserve the segment above.Please mail me if you have any question.
 */
import { login } from '@/api/ht.login';
import { loadDict } from '@/api/ht.common';
import config from '@/store/config';
import * as auth from '@/utils/ht.auth';
import path from 'path';

const state = {
  token: auth.getToken(),
  refreshToken: auth.getRefreshToken(),
  language: auth.getLanguage(),
  loginAccount: auth.getLoginAccount(),
  loginUuid: auth.getLoginUuid(),
  userInfo: auth.getUserInfo(),
  rtflag: auth.getRtflag(),
  navMenus: auth.getNavMenus(),
  isLoadDict: auth.getIsLoadDict(),
  dict: auth.getDict(),
  dictCache: {}, // 字典缓存
  avatar: path.resolve(__dirname, '/assets/images/')
};

const mutations = {
  [config.SET_TOKEN]: (state, token) => {
    state.token = token;
  },
  [config.SET_REFRESH_TOKEN]: (state, refreshToken) => {
    state.refreshToken = refreshToken;
  },
  [config.SET_RTFLAG]: (state, rtflag) => {
    state.rtflag = rtflag;
  },
  [config.SET_LANGUAGE]: (state, language) => {
    state.language = language;
  },
  [config.SET_LOGIN_ACCOUNT]: (state, loginAccount) => {
    state.loginAccount = loginAccount;
  },
  [config.SET_LOGIN_UUID]: (state, loginUuid) => {
    state.loginUuid = loginUuid;
  },
  [config.SET_USERINFO]: (state, userinfo) => {
    state.userInfo = userinfo;
  },
  [config.SET_NAVMENUS]: (state, navMenus) => {
    state.navMenus = navMenus;
  },
  [config.SET_DICT]: (state, dict) => {
    state.dict = dict;
  },
  [config.SET_DICT_CACHE]: (state, dictCache) => {
    state.dictCache[dictCache.key] = dictCache.dict;
  }
};

const actions = {
  /**
     * @todo: 登录
     * @author: andy.ten@tom.com & xukaixing@hotmail.com
     * @Date: 2022-03-24 09:53:52
     * @param {*} commit
     * @param {*} userInfo
     */
  async handleLogin({ commit }, loginInfo) {
    try {
      const { datas } = await login(loginInfo);

      // 设置browser storage
      const { token, refreshToken, loginAccount, loginUuid } = datas.access_token;
      const userInfo = JSON.stringify(datas.userinfo);
      auth.setToken(token);

      if (refreshToken) {
        // fixme: 每次登录都需要刷新token 2022-04-24
        auth.setRtflag(0);
        auth.setRefreshToken(refreshToken);
      }
      auth.setLoginAccount(loginAccount);
      auth.setLoginUserId(datas.userinfo.id);
      auth.setLoginUuid(loginUuid);
      auth.setIsLoadDict(datas.is_loaddic);
      auth.setLanguage('zh');
      auth.setUserInfo(userInfo);

      // 设置 vuex state
      commit(config.SET_TOKEN, token);
      commit(config.SET_LOGIN_ACCOUNT, loginAccount);
      commit(config.SET_LOGIN_UUID, loginUuid);
      commit(config.SET_USERINFO, userInfo);
      if (refreshToken) {
        // fixme: 每次登录都需要刷新token 2022-04-24
        commit(config.SET_RTFLAG, 0);
        commit(config.SET_REFRESH_TOKEN, refreshToken);
      }
      commit(config.SET_LANGUAGE, 'zh');
    } catch (error) {
      console.log('Login => ', error);
      // Notify.alert(error, 'error');
      // 向上抛出
      return Promise.reject(error);
    }
  },
  // 登出
  async handleLogout({ commit }) {
    commit(config.SET_TOKEN, '');
    auth.removeToken();
    auth.clearSStorage();
  },
  // remove token
  async resetToken({ commit }) {
    commit(config.SET_TOKEN, '');
    auth.removeToken();
  },

  // refresh token
  async refreshToken({ commit }, newToken) {
    commit(config.SET_TOKEN, newToken);
    auth.setToken(newToken);
  },

  /**
   * @todo: 设置nav menus
   * @author: andy.ten@tom.com & xukaixing@hotmail.com
   * @Date: 2022-03-28 08:37:15
   * @param {*} commit
   * @param {*} navMenus
   */
  setNavMenus({ commit }, navMenus) {
    commit(config.SET_NAVMENUS, navMenus);
    auth.setNavMenus(navMenus);
  },
  async loadAllDict({ commit }) {
    const { datas } = await loadDict();
    auth.setDict(datas);
    commit(config.SET_DICT, datas);
  },
  addDictCache({ commit }, dictCache) {
    commit(config.SET_DICT_CACHE, dictCache);
  }
};

export default {
  namespaced: true, // module设置命名空间
  state,
  mutations,
  actions
};
